package com.geeke.config.redisson;

import org.redisson.Redisson;
import org.redisson.api.NameMapper;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.geeke.config.cache.RedisConfig;;
/**
 * redis分布式事务锁
 */
@Configuration
public class RedissonConfig {
    @Value("${spring.redis.host}")
    private String redisAddress;

    @Bean
    public RedissonClient redissonClient(RedisConfig redisConfig) {
        String prefix = String.format("%s:lock:", redisConfig.getApplicationName());
        NameMapper nameMapper = new PrefixNameMapper(prefix);
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + redisConfig.getHost() + ":" + redisConfig.getPort()) //redis服务器地址
                .setDatabase(redisConfig.getDatabase()) //制定redis数据库编号
                // .setUsername("")
                .setPassword(redisConfig.getPassword()) // redis用户名密码
                //    .setConnectionMinimumIdleSize(10) //连接池最小空闲连接数
                //    .setConnectionPoolSize(50) //连接池最大线程数
                //    .setIdleConnectionTimeout(60000) //线程的超时时间
                //    .setConnectTimeout(6000) //客户端程序获取redis链接的超时时间
                .setTimeout(redisConfig.getTimeout()) //响应超时时间
                .setNameMapper(nameMapper);   // 前缀处理
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}
